home *** CD-ROM | disk | FTP | other *** search
- /* Copyright 1996 Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* normals.c - Show the effect of lighting on a flat surface
- * when using face normals and vertex normals. The red
- * lines represent the directions of the normal vectors.
- *
- * Left Mousebutton - move eye position
- * Middle Mousebutton - change twist angle
- * Right Mousebutton - move up / down to zoom in / out
- * Escape Key - exit the program
- */
-
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include "3d.h"
- #include <math.h>
- #include <stdio.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid mouse( GLint, GLint, GLint, GLint );
- GLvoid motion( GLint, GLint );
-
- GLvoid drawNormal( GLfloat, GLfloat, GLfloat, GLfloat, GLfloat, GLfloat );
-
- void resetView( GLvoid );
- void polarView( GLfloat, GLfloat, GLfloat, GLfloat );
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static enum actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
- static GLint action;
-
- static GLdouble xStart = 0.0, yStart = 0.0;
-
- static GLfloat fovy, near, far, distance, twistAngle, incAngle, azimAngle;
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- /* create a window that is 1/4 the size of the screen */
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width / 4, height / 4 );
- glutInitWindowSize( (width / 2) - 4, height / 2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutMouseFunc( mouse );
- glutMotionFunc( motion );
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - demonstrate face and vertex normals\n\n"
- "Left Mousebutton - move eye position\n"
- "Middle Mousebutton - change twist angle\n"
- "Right Mousebutton - move up / down to zoom in / out\n"
- "Escape Key - exit the program\n\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- glEnable( GL_DEPTH_TEST );
-
- fovy = 60.0; /* field of view in Y */
- near = 3.0; /* Near clipping plane location */
- far = 12.0; /* Far clipping plane location */
-
- resetView();
-
- /* Turn on a default light */
- glEnable( GL_LIGHT0 );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- static GLint buttons_down = 0;
-
- if (state == GLUT_DOWN) {
- switch (button) {
- case GLUT_LEFT_BUTTON:
- action = MOVE_EYE;
- break;
- case GLUT_MIDDLE_BUTTON:
- action = TWIST_EYE;
- break;
- case GLUT_RIGHT_BUTTON:
- action = ZOOM;
- break;
- }
-
- /* Update the saved mouse position */
- xStart = x;
- yStart = y;
- } else {
- if (--buttons_down == 0)
- action = MOVE_NONE;
- }
-
- }
-
- GLvoid
- motion( GLint x, GLint y )
- {
- switch (action) {
- case MOVE_EYE:
- /* Adjust the eye position based on the mouse position */
- azimAngle += (GLdouble) (x - xStart);
- incAngle -= (GLdouble) (y - yStart);
- break;
- case TWIST_EYE:
- /* Adjust the eye twist based on the mouse position */
- twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
- break;
- case ZOOM:
- /* Adjust the eye distance based on the mouse position */
- distance -= (GLdouble) (y - yStart)/10.0;
- break;
- default:
- printf("unknown action %d\n", action);
- }
-
- /* Update the stored mouse position for later use */
- xStart = x;
- yStart = y;
-
- glutPostRedisplay();
- }
-
- void
- resetView( GLvoid )
- {
- distance = near + (far - near) / 2.0;
- twistAngle = 0.0; /* rotation of viewing volume (camera) */
- incAngle = 60.0;
- azimAngle = 0.0;
- fovy = 60.0; /* Field of view in Y angle */
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( fovy, aspect, near, far );
- glMatrixMode( GL_MODELVIEW );
- }
-
- void
- polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
- GLfloat twist)
- {
- glTranslatef( 0.0, 0.0, -distance);
- glRotatef( -twist, 0.0, 0.0, 1.0);
- glRotatef( -incidence, 1.0, 0.0, 0.0);
- glRotatef( -azimuth, 0.0, 0.0, 1.0);
- }
-
- GLvoid
- drawNormal( GLfloat px, GLfloat py, GLfloat pz,
- GLfloat nx, GLfloat ny, GLfloat nz )
- {
- GLdouble normal[3], cross[3], zaxis[3];
- GLdouble angle;
-
- #define DegreesToRadians (M_PI / (GLfloat) 180.0)
-
- glColor3f( 1.0, 0.0, 0.0 );
- glPushMatrix();
- glTranslatef( px, py, pz );
- glBegin( GL_LINES );
- glVertex3f( 0.0, 0.0, 0.0 );
- glVertex3f( nx, ny, nz );
- glEnd();
-
- /* normalize the normal vector */
- normal[0] = nx; normal[1] = ny; normal[2] = nz;
- normalize( normal );
-
- /* determine angle between z axis and normal */
- zaxis[0] = 0; zaxis[1] = 0; zaxis[2] = 1;
- angle = acos( dot3( zaxis, normal ) )/DegreesToRadians;
-
- if ( angle != 0.0 ) {
- /* find the axis of rotation */
- crossprod( zaxis, normal, cross );
- glRotatef( angle, cross[0], cross[1], cross[2] );
- }
- /* move to end of normal vector */
- glTranslatef( 0.0, 0.0, 1.0 );
- glutSolidCone( 0.05, 0.1, 8, 8 );
- glPopMatrix();
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glPushMatrix();
-
- polarView( distance, azimAngle, incAngle, twistAngle );
-
- /* Draw a lit flat-shaded triangle on the left */
- glPushMatrix();
- glTranslatef( -2.0, 0.0, 0.0 );
-
- /* Turn on lighting computations */
- glEnable( GL_LIGHTING );
-
- glBegin( GL_TRIANGLES );
- glNormal3f( 0.0, 0.0, 1.0 );
- glVertex2f( 0.0, 1.0 );
- glVertex2f( -0.866, -0.50 );
- glVertex2f( 0.866, -0.50 );
- glEnd();
-
- /* Turn off lighting computations */
- glDisable( GL_LIGHTING );
-
- /* Draw an unlit line representing the face normal */
- drawNormal( 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 );
- glPopMatrix();
-
- /* Draw a lit, smooth-shaded triangle on the right */
- glPushMatrix();
- glTranslatef( 2.0, 0.0, 0.0 );
-
- /* Turn on lighting computations */
- glEnable( GL_LIGHTING );
-
- glBegin( GL_TRIANGLES );
- glNormal3f( 0.0, 0.0, 1.0 );
- glVertex2f( 0.0, 1.0 );
- glNormal3f( 0.455, 0.796, 0.398 );
- glVertex2f( -0.866, -0.50 );
- glNormal3f( 0.162, 0.566, 0.808 );
- glVertex2f( 0.866, -0.50 );
- glEnd();
-
- /* Turn off lighting computations */
- glDisable( GL_LIGHTING );
-
- /* Draw unlit lines representing the vertex normals */
- drawNormal( 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 );
- drawNormal( -0.866, -0.50, 0.0, 0.455, 0.796, 0.398 );
- drawNormal( 0.866, -0.50, 0.0, 0.162, 0.566, 0.808 );
- glPopMatrix();
-
-
- glPopMatrix();
-
- glutSwapBuffers();
- }
-